//
//  YBCloudComputerTableHeader.swift
//  Moonlight
//
//  Created by Penlon Kim on 2023/6/8.
//  Copyright © 2023 Moonlight Game Streaming Project. All rights reserved.
//

import UIKit
import SnapKit
import CHIPageControl

class YBCloudComputerTableHeader: UIView {
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.addSubview(cardView)
        cardView.addSubview(pageControl)
        
        cardView.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(10).priority(.high)
            make.top.equalTo(10)
            make.bottom.equalTo(-18).priority(.medium)
            make.height.equalTo(UIDevice.bk_isiPhone ? 130 : 180).priority(.high)
        }
        
        pageControl.snp.makeConstraints { make in
            make.bottom.equalTo(-9)
            make.centerX.equalToSuperview()
            make.height.equalTo(4)
        }
        
        cardView.reloadData()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - lazy
    lazy var cardView: BKCardView = {
        let cardView = BKCardView()
        cardView.pagingEnabled = true
        cardView.isInfinite(true)
        cardView.isAutoScroll(true)
        cardView.delegate = self
        cardView.dataSource = self
        cardView.setStyle(by: .center)
        cardView.scrollTimeInterval(3.0)
        cardView.setFlowlayout(wScale: 1.0, hScale: 1.0)
        cardView.register(cellWithClass: YBCloudComputerBannerCell.self)
        cardView.bk_addCornerBorder(radius: 10, borderWidth: 0.1, borderColor: .clear)
        return cardView
    }()
    
    private lazy var pageControl: CHIPageControlJalapeno = {
        let pageControl = CHIPageControlJalapeno()
        pageControl.radius = 2
        pageControl.tintColor = UIColor.black.withAlphaComponent(0.3)
        pageControl.currentPageTintColor = .white
        pageControl.numberOfPages = 1
        return pageControl
    }()
    
}

// MARK: - BKCardView代理
extension YBCloudComputerTableHeader: BKCardViewDataSource, BKCardViewDelegate {
    
    func cardSwitchNumberOfCard() -> Int {
        pageControl.numberOfPages = 3
        return 3
    }
    
    func cardSwitchCellForItemAtIndex(index: Int) -> UICollectionViewCell {
        let cell = cardView.dequeueReusableCell(withClass: YBCloudComputerBannerCell.self, for: index)
        return cell
    }
    
    func cardSwitchDidSelectedAtIndex(index: Int) {
        
    }
    
    func cardSwitchDidScrollToIndex(index: Int) {
        let number = pageControl.numberOfPages
        pageControl.set(progress: index%(number+1), animated: true)
    }
    
}
